package com.hh.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.demo.entity.Book;
import com.hh.demo.entity.Bookinfo;
import com.hh.demo.service.BookService;
import com.hh.demo.service.BookinfoService;
import com.hh.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BookController {
    @Resource
    private BookService bookService;
    @Resource
    private UserService userService;
    @Resource
    private BookinfoService bookinfoService;
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    //借阅图书：插入一条图书借阅数据*
    @RequestMapping("/insertBook")
    @ResponseBody
    public void insertBook(String userCard,String bookCard){
        Bookinfo bookInfo = bookinfoService.queryBookInfo(bookCard);
        String userName = userService.queryUser(userCard);
        bookService.insertBook(new Book(
                bookInfo.getBookNo(),
                bookInfo.getBookName(),
                bookInfo.getAuthor(),
                userName,
                format.format(date),
                null,
                "已借阅",
                bookInfo.getBookNum()-1
        ));
    }
    //归还图书：修改图书数据*
    @PostMapping("/updateBook")
    @ResponseBody
    public void updateBook(String userCard){
        String borrower = userService.queryUser(userCard);
        //根据借阅人来修改信息
        String returnDate = format.format(date);
        bookService.updateBook(borrower ,returnDate);
    }
    //查询全部*
    @RequestMapping("/queryAllbook")
    @ResponseBody
    public List<Book> queryAll(){
        return bookService.queryAll();
    }
    //按照借阅人查询数量*
    @RequestMapping("/queryCount")
    @ResponseBody
    public List<Book> queryCount(){
        return bookService.queryCount();
    }
    //查询全部信息*
    @RequestMapping("/queryAll")
    public String queryAll(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<Book> list = bookService.queryAll();
        PageInfo<Book> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
        return "queryAll";
    }
//---------------------------------------------------------------------------
    //查询数据库中的数据总条数，返回该条数
    @RequestMapping("/queryPageNumTotal")
    public Integer queryPageNumTotal(){
        return bookService.queryAll().size();
    }
    //查询全部*
    @RequestMapping("/queryAllbookInfo")
    public List<Book> queryAllbook(){
        return bookService.queryAll();
    }
    //根据书籍编号查多条数据*
    @RequestMapping("/queryBookBybookNo")
    public void queryBookBybookNo(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,String bookNo) {
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<Book> list = bookService.queryBookBybookNo(bookNo);
        PageInfo<Book> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
    }
    //根据借阅人查多条数据
    @RequestMapping("/queryBookByBorrower")
    public void queryBookByBorrower(String borrower,Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        System.out.println(borrower+"-------------------------------");
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<Book> list = bookService.queryBookByBorrower(borrower);
        PageInfo<Book> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
    }
    //根据借阅时间查多条数据
    @RequestMapping("/queryBookByborrowTime")
    public void queryBookByborrowTime(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,String borrowTime) {
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<Book> list = bookService.queryBookByborrowTime(borrowTime);
        PageInfo<Book> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
    }
}
